package com.kxf.androidtestdemo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.kxf.androidtestdemo.R;
import com.kxf.androidtestdemo.bean.HanoiItem;
import com.kxf.androidtestdemo.view.StackLayout;

import java.util.ArrayList;
import java.util.List;

public class HanoiActivity extends AppCompatActivity {
    private StackLayout sl_a = null;
    private StackLayout sl_b = null;
    private StackLayout sl_c = null;
    private static int NUM = 10;
    private int height;
    private int width;
    private List<HanoiItem> itemA = new ArrayList<>();
    private List<HanoiItem> itemB = new ArrayList<>();
    private List<HanoiItem> itemC = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hanoi);
        initView();
    }

    private void initView() {

        sl_a = findViewById(R.id.sl_a);
        sl_b = findViewById(R.id.sl_b);
        sl_c = findViewById(R.id.sl_c);
        ViewTreeObserver observer = sl_a.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                sl_a.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                height = sl_a.getMeasuredHeight();
                width = sl_a.getMeasuredWidth();
                Log.d("HanoiActivity", "height=" + height);
                Log.d("HanoiActivity", "width=" + width);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int itemH = height / (NUM + 1);
                        for (int i = 0; i < NUM; i++) {
                            TextView tv = new TextView(HanoiActivity.this);
                            tv.setText((NUM - i) + "");
                            tv.setHeight(itemH);
                            tv.setWidth(width - 10 * i);
                            tv.setGravity(Gravity.CENTER);
                            tv.setTextColor(Color.RED);
                            tv.setBackgroundColor(Color.rgb(255 / NUM * i, 255 / NUM * i, 255 / NUM * i));
                            sl_a.addView(tv);
                        }
                        sl_a.requestLayout();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                hanoit(NUM, sl_a, sl_b, sl_c);
                            }
                        }).start();
                    }
                });
            }
        });

    }

    private void move(final StackLayout x, final StackLayout y) {
//        System.out.printf("%c-->%c",x,y);
//        System.out.print("\n");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                View view = x.getChildAt(x.getChildCount() - 1);
                x.removeView(view);
                y.addView(view);
            }
        });
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void hanoit(final int n, final StackLayout one, final StackLayout two, final StackLayout three) {     //将n个盘子从第一座借助第二座移到第三座

        if (n == 1) {                           //如果只有一个盘子
            move(one, three);
        } else {
            hanoit(n - 1, one, three, two);      //将一上的盘子借助三移到二上
            move(one, three);
            hanoit(n - 1, two, one, three);      //将二上的盘子借助一移到三上
        }
    }
//    public static void main(String[] args) {
//        int m;
//        System.out.println("请输入你要移动的盘子数：");
//        Scanner s=new Scanner(System.in);
//        m=s.nextInt();
//        System.out.println("移动"+m+"个盘子的步骤如下");
//        hanoit(m,'A','B','C');
//
//    }

}
